package com.naton.common.utils.picture;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.luck.picture.lib.animators.AnimationType;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.engine.CompressEngine;
import com.luck.picture.lib.engine.CropEngine;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.style.PictureSelectorStyle;
import com.naton.common.R;
import com.naton.common.bean.ImageItem;
import com.naton.common.widget.SelectDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @time: 2022/1/19
 * @author: lifuling
 * @version: v1.0
 * @file_desc: 图片选取及预览工具类
 */
public class PictureSelectorUtils {

    public static SelectDialog showPicSelect(Activity activity , List<LocalMedia> data
            , int maxSelectNum , PicSelectorListener picSelectorListener){
        List<String> names = new ArrayList<>();
        names.add("相册选择");
        names.add("拍照");
        SelectDialog dialog = new SelectDialog(activity, R.style.transparentFrameWindowStyle, new SelectDialog.SelectDialogListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1: {
                        //用相机
                        selectPicFromCamara(activity, picSelectorListener);
                    }
                    break;
                    case 0: {
                        //从相册选取
                        selectPicFromGallery(activity,  maxSelectNum , data, picSelectorListener);
                    }
                    break;
                }
            }
        }, names);
        if (!activity.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }

    /**
     * 单独拍照
     */
    public static void selectPicFromCamara(Context context , final  PicSelectorListener picSelectorListener){
        PictureSelector.create(context)
                .openCamera(SelectMimeType.ofImage())
//                .setCameraInterceptListener(getCustomCameraEvent())
                .setCropEngine(getCropEngine(context , false))
                .setCompressEngine(getCompressEngine(context , false))
                .setSandboxFileEngine(new MeSandboxFileEngine())
                .isOriginalControl(false)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        pressResult(result , picSelectorListener);
                    }

                    @Override
                    public void onCancel() {
                        picSelectorListener.onCancel();
                    }
                });
    }

    /**
     * 从相册获取
     * @param context
     */
    public static void selectPicFromGallery(Context context , int selectLimitCount
            , List<LocalMedia> selectData , PicSelectorListener picSelectorListener){
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofAll())
                //设置主题样式
                .setSelectorUIStyle(getPictureSelectorStyle())
                .setImageEngine(GlideEngine.createGlideEngine())
                //设置自定义裁剪引擎
                .setCropEngine(getCropEngine(context,false))
                //设置自定义压缩引擎
                .setCompressEngine(getCompressEngine(context , false))
                .setSandboxFileEngine(new MeSandboxFileEngine())
                //拦截摄像头点击事件，用户可以实现自己的摄像头框架
//                .setCameraInterceptListener(getCustomCameraEvent())
                //自定义限制提示
                .setSelectLimitTipsListener(new MeOnSelectLimitTipsListener())
                //拦截媒体编辑点击事件，用户可以实现自己的编辑媒体框架
//                .setEditMediaInterceptListener(getCustomEditMediaEvent())
                //设置多选 或者单选
                .setSelectionMode(selectLimitCount > 1 ? SelectModeConfig.MULTIPLE : SelectModeConfig.SINGLE)
                //显示资源的创建时间线
                .isDisplayTimeAxis(false)
                //查询指定目录
                .isOnlyObtainSandboxDir(false)
                //设置是否分页模式
                .isPageStrategy(true)
                //是否开启原图功能
                .isOriginalControl(false)
                //显示或隐藏拍摄
                .isDisplayCamera(true)
                //是否开启点击声音
                .isOpenClickSound(true)
                //图片视频同选
                .isWithSelectVideoImage(false)
                //全屏预览
                .isPreviewFullScreenMode(true)
                //预览缩放效果
                .isPreviewZoomEffect(true)
                //是否预览图片
                .isPreviewImage(true)
                //是否预览视频
//                .isPreviewVideo(true)
                //是否预览音频
//                .isPreviewAudio(true)
                //是否显示蒙层(达到最大可选数量)
                .isMaxSelectEnabledMask(true)
                //单选模式直接返回
                .isDirectReturnSingle(false)
                //选择最大文件数
                .setMaxSelectNum(selectLimitCount)
                .setRecyclerAnimationMode(AnimationType.DEFAULT_ANIMATION)
                //是否显示Gif图片
                .isGif(false)
                //设置选中的图片集合
                .setSelectedData(selectData == null ? new ArrayList<>() : selectData)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        pressResult(result , picSelectorListener);
                    }

                    @Override
                    public void onCancel() {
                        picSelectorListener.onCancel();
                    }
                });
    }

    /**
     * 统一处理
     * @param result
     * @param picSelectorListener
     */
    private static void pressResult(ArrayList<LocalMedia> result, PicSelectorListener picSelectorListener) {
        List<ImageItem> imageItems = new ArrayList<>();
        if (result != null) {
            for (LocalMedia media : result) {
                ImageItem imageItem = new ImageItem();
                String path = "";
                if (media.isCut() && !media.isCompressed()) {
                    // 裁剪过
                    path = media.getCutPath();
                } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                    // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                    path = media.getCompressPath();
                } else {
                    // 原图
                    path = media.getPath();
                }
                imageItem.settPath(path);
                imageItem.setoPath(path);
                imageItems.add(imageItem);
            }
        }
        picSelectorListener.onResult(imageItems);
    }

    /**
     * 获取自定义压缩引擎
     * @param context
     * @return
     */
    private static CompressEngine getCompressEngine(Context context , boolean isCompress) {
        if (isCompress){
            return new ImageCompressEngine(context);
        }else {
            return null;
        }
    }

    /**
     * 获取自定义裁剪引擎
     * @return
     */
    private static CropEngine getCropEngine(Context context , boolean crop) {
        if (crop){
            //裁剪
            return new ImageCropEngine(context , true);
        }else {
            //不裁剪
            return null;
        }
    }

    /**
     * 统一设置主题样式
     */
    private static PictureSelectorStyle pictureSelectorStyle;
    private static PictureSelectorStyle getPictureSelectorStyle() {
        if (pictureSelectorStyle == null){
            pictureSelectorStyle = new PictureSelectorStyle();
        }
        return pictureSelectorStyle;
    }

}
